<?php
require ('lib/verify.php');
require ('lib/has_char.php');
require ('lib/is_approved.php');

$_category = "wardkp";
$_title = "Raids";
require ('lib/head.php');

boxTop ( "$_category: $_title" );
if ($_GET ['a'] == "view") {
    $raid_id = $_GET ["id"];
    $system_id = $system ["system_id"];
	
    $sql = "select *, wm_instances.* from wm_raids left join wm_instances on wm_instances.instance_id = raid_instance where raid_id = $raid_id";
    $result = $db->query ( $sql );
    $raid = $db->fetch_assoc ( $result );
	
    if ($db->num_rows ( $result ) < 1) {
        header ( "Location: $war_http/error-wrong-system.php" );
        exit ();
    }
	
    echo '<a href="loot-instance.php?id='.$raid["instance_id"].'"><b><'.$raid["instance_name"].'</b></a>';
    echo ', '.date("D d M Y", strtotime($raid["raid_date"]));
    echo '<b>T'.$system["system_tier"].'R'.$raid["raid_id"].'</b>';
    echo '<br\><br\>';
	
    $sql = "select *, wm_bosses.* from wm_credits left join wm_bosses on wm_bosses.boss_id = credit_boss where credit_raid = '" . $raid ["raid_id"] . "' order by credit_date asc";
    $res = $db->query ( $sql );
    $i = 0;
    $j = 0;
    while ( $cred = $db->fetch_assoc ( $res ) ) {
        $i ++;
        $j ++;
        $credit_id = $cred ['credit_id'];
        $btitle = $cred ["boss_name"] . ', ' . date ( "H:i", strtotime ( $cred ["credit_date"] ) );
        boxTop ( $btitle );
        echo '<a href="javascript:unhide(\'hide' . $i . '\')">Show/Hide Loot</a><br \><div id="hide' . $i . '"  class="hidden"><br \>';
		
        //
        //Start loot
        //
		

        $sqlloot = "select *, wm_characters.*, wm_item_values.* from wm_loots left join wm_characters on wm_characters.character_id = loot_character left join item_cache on wm_loots.loot_item = item_cache.item_id left join wm_item_values on wm_loots.loot_item = wm_item_values.item_id where loot_system_id = $system_id and loot_credit = $credit_id and wm_item_values.item_system_id=$system_id  order by item_cache.item_name asc";
		
        $resultloot = $db->query ( $sqlloot );
        if (! $db->num_rows ( $resultloot )) {
            echo '<i>No loot</i><br /><br />';
            //echo $system_id . "<br />";
            //echo $credit_id . "<br /><br />";
        }
		
        while ( $loot = $db->fetch_assoc ( $resultloot ) ) {
            echo cclass ( $loot ["character_class"] ) . show_link ( array ("item_id" => $loot ["loot_item"] ) );
            echo '<a href="character.php?id=' . $loot ["loot_character"] . '">' . $loot ["character_name"] . '</a> looted for '.bid2text($loot["loot_level"]);
	    if ($loot["loot_bid"] > 1) {
		echo '(bid: ' . number_format ( $loot ["loot_bid"], 2 ) . ' dkp) (' . number_format ( $loot ["item_value"], 2 ) . ' paid)';
            }
	    if ($character ["character_admin"]) {
                $did_loot [$loot ["loot_character"]] = 1;
                //echo '('.adminfeature().' <a href="remove-loot.php?lid='.$loot["loot_id"].'&id='.$loot["loot_item"].'&cid='.$credit_id.'&pid='.$loot["loot_character"].'">remove</a>)';
            }
            echo "<br />\n<br />\n";
        }
        //
        //End loot
        //
        echo '</div>';
		
        echo '<a href="javascript:unhide(\'hidej' . $j . '\')">Show/Hide Attendance</a><br \><div id="hidej' . $j . '"  class="hidden">';
        //
        //Start attendance
        //
		

        $sqlatt = "select * , wm_characters.* from wm_attendees left join wm_characters on wm_characters.character_id = wm_attendees.attendee_character where attendee_system_id = $system_id and attendee_credit = $credit_id and attendee_satout = 0 group by character_id order by character_class asc, character_name asc";
        $resultatt = $db->query ( $sqlatt );
		
        echo '<i>' . $db->num_rows ( $resultatt ) . 'characters</i><br/>';
		
        while ( $attendee = $db->fetch_assoc ( $resultatt ) ) {
            echo cclass ( $attendee ["character_class"] );
            echo ' <a href="character.php?id=' . $attendee ["character_id"] . '">' . $attendee ["character_name"] . '</a>';
            if ($character ["character_admin"]) {
                if ($did_loot [$attendee ["character_id"]] != 1) {
                }
            }
            echo "<br />\n";
        }
		
        while ( $attendee = $db->fetch_assoc ( $resultatt ) ) {
            echo cclass ( $attendee ["character_class"] );
            echo ' <a href="character.php?id=' . $attendee ["character_id"] . '">' . $attendee ["character_name"] . '</a>';
            if ($character ["character_admin"]) {
                echo '(';
                echo adminfeature ();
                echo ' <a href="remove-attendee.php?id=' . $attendee ["character_id"] . '&cid=' . $credit_id . '">remove</a>)';
            }
        }
		
        //
        //End attendance
        //
		

        echo '</div>';
        boxBot ();
    }
	
    //
    //Start default raid view
    //


 } else {
    $system_id = $system ["system_id"];
    if(isset($_GET['nr'])) $n_raids = $_GET['nr'];
    else $n_raids = 1;






    echo 'Raids that have been registered on this tier:<br>' . '<i>or <a href="administrate.php?p=raid&a=add">add a raid</a>.</i><br /><br />' . '<table>' . '<tr>' . '<th width="50">View</th>' . '<th>Where</th>' . '<th>When</th>' . '<th>Bosses</th>' . '<th width="40">ID</th>' . '</tr>';

    $per_page = 20;
    $offset = ($n_raids -1) * $per_page;
    $sql = "select *, wm_instances.instance_name
			from wm_raids left join wm_instances on wm_raids.raid_instance = wm_instances.instance_id 
			where wm_raids.raid_system_id = $system_id order by raid_date desc limit $offset, $per_page";
    $result = $db->query ( $sql );

    $i = 0;

    while ( $raid = $db->fetch_assoc ( $result ) ) {
        $i ++;
        $sqlb = "select *, wm_bosses.* from wm_credits left join wm_bosses on wm_bosses.boss_id = credit_boss where credit_raid = '" . $raid ["raid_id"] . "' and credit_system_id = $system_id order by credit_date asc";
        $resb = $db->query ( $sqlb );
        $ib = 0;
        //$bosses = array();
        while ( $cred = $db->fetch_assoc ( $resb ) ) {
            $ib ++;
            $bosses [] = $cred ['boss_name'] . '| <i>(' . date ( "H:i", strtotime ( $cred ["credit_date"] ) ) . ')</i><br />';
        }

        echo '<tr>';
        echo '<td align="center">';
        echo '<a href="raids.php?a=view&id=' . $raid ["raid_id"] . '"><img src="' . $war_http . '/images/list.gif" alt="View"></a> ';
        echo '</td>';
        echo '<td>' . $raid ["instance_name"] . '</td>';
        echo '<td>' . date ( "D d M Y", strtotime ( $raid ["raid_date"] ) ) . '</td>';
        echo '<td>Defeated: ' . $ib;
        echo ' <a href="javascript:unhide(\'hide' . $i . '\');">Show/hide</a><br \><div id="hide' . $i . '"  class="hidden">';
        if ($bosses != true) {
            echo "No bosses killed.";
        }
        else {
            foreach ( $bosses as $bos ) {
                list ( $boss, $time ) = explode ( '|', $bos );
                echo $boss . $time;
            }
        }
        echo '</div></td>';
        echo '<td>T' . $system ["system_tier"] . 'R' . $raid ["raid_id"] . '</td>';
        echo '</tr>';
        unset ( $bosses );
    }
    echo '</table>';

    $query = "SELECT * FROM wm_raids WHERE raid_system_id = $system_id";
    $result = $db->query($query);
    $numr = $db->num_rows($result);

    $maxPage = ceil($numr/$per_page);

    $self = $_SERVER['PHP_SELF'];
    $nav  = '';

    for($page = 1; $page <= $maxPage; $page++) {
        if ($page == $n_raids) $nav .= " $page ";
        else $nav .= " <a href=\"$self?nr=$page\">$page</a> ";
    }



    if ($n_raids > 1){
        $page  = $n_raids - 1;
        $prev  = " <a href=\"$self?nr=$page\">[Prev]</a> ";
    }
    else $prev  = '&nbsp;';

    if ($n_raids < $maxPage){
        $page = $n_raids + 1;
        $next = " <a href=\"$self?nr=$page\">[Next]</a> ";
    }
    else $next = '&nbsp;';
    echo "<div align=center>$prev $nav $next</div>";
 }
boxBot ();
?>

<?php
require ('lib/foot.php');
?>

